#include <bits/stdc++.h>
#define maxn 200010
#define maxm 1000010
#define ll long long
using namespace std;
#define writeln(X) write((X)), putchar('\n')
template < typename T >
inline void read(T &X)
{
    X = 0; bool f = false; char ch = getchar();
    while (!isdigit(ch)) {f |= ch == '-'; ch = getchar();}
    while (isdigit(ch)) {X = (X * 10) + (ch ^ 48); ch = getchar();}
    X = f ? -X : X;
    return;
}
template < typename T >
inline void write(T X)
{
    if (X == 0) {putchar('0'); return;}
    if (X < 0) {putchar('-'); X = -X;}
    static char ch[20], cnt = 0;
    while (X) {*(ch + cnt++) = X % 10; X /= 10;}
    while (cnt) putchar(*(ch + --cnt) ^ 48);
    return;
}
int T;
int n, cnt, ans;
int a[maxn];
int pre[maxn], col[maxm];
ll dp[maxn][2][2];
int main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    read(T);
    while (T--)
    {
        ans = 0; cnt = 0;
        memset(pre, 0, sizeof(pre)); memset(col, 0, sizeof(col)); memset(dp, 0, sizeof(dp));
        read(n);
        for (int i = 1; i <= n; ++i)
        {
            read(a[cnt + 1]);
            if (a[cnt] == a[cnt + 1]) ans += a[cnt];
            else ++cnt;
        }
        n = cnt;
        for (int i = 1; i <= n; ++i)
        {
            if (col[a[i]]) pre[i] = col[a[i]];
            col[a[i]] = i;
        }
        for (int i = 1; i <= n; ++i)
        {
            dp[i][0][0]  = max(dp[i - 1][0][0], dp[i - 1][0][1]), dp[i][1][1] = max(dp[i - 1][1][0], dp[i - 1][1][1]);
            dp[i][1][0]  = max(dp[i - 1][0][0], dp[i - 1][0][1]), dp[i][0][1] = max(dp[i - 1][1][0], dp[i - 1][1][1]);
            if (pre[i]) dp[i][0][1] = max(dp[i][0][1], dp[pre[i] + 1][1][0] + a[i]), dp[i][1][0] = max(dp[i][1][0], dp[pre[i] + 1][0][1] + a[i]);
        }
        writeln(ans + max(max(dp[n][0][0], dp[n][0][1]), max(dp[n][1][0], dp[n][1][1])));
    }
    return 0;
}